//光配连接
var LogicConnectChart = Chart = {
    DeviceWidth:240,//橱柜宽度
    DeviceSpace:200,//设备间隔
    DeviceVerSpace:60,//设备竖向间隔
    DeviceColumn:3,
    TitleHeight:40,//橱柜标题高度
    TitleBg:'#00a698',//橱柜标题背景
    TitleFontSize:'20',//橱柜标题字体大小
    DeviceHeight:60,
    PortHeight:50,
    PortVerSpace:2,
    iconWidth : 30,
    iconHeight : 30,
    parseFrom: function(unit_id, logicConnectData){
        /*
        {
            "count": 3,
            "list": [
                {
                    "EXT_CB_NAME": "gocb0",
                    "EXT_IED_DESC": "220kV镇江电厂线智能终端B套(PCS-222B)",
                    "EXT_PORT_DIRECTION": "Tx",
                    "EXT_PORT_NAME": "B01-A",
                    "EXT_PORT_PLUG": "LC",
                    "EXT_UNIT_ID": 41,
                    "FIRST_FCDA": "RPIT/Q0AXCBR1.Pos.stVal",
                    "FIRST_FCDA_DESC": "A相断路器位置",
                    "FIRST_INTADDR": "PIGO01/GOINGGIO1.DPCSO2.stVal",
                    "FIRST_INTADDR_DESC": "断路器位置A相",
                    "ID": 47,
                    "IED_DESC": "220kV镇江电厂线测控(PSR 663U)",
                    "PORT_DIRECTION": "Rx",
                    "PORT_NAME": "4X-A",
                    "PORT_PLUG": "LC",
                    "TYPE": "GO",
                    "UNIT_ID": 40
                },
            ]
        }
        */
        var chart = {};
        chart.x=0;
        chart.y=0;
        chart.unit_id = unit_id;
        chart.logicConnectData = logicConnectData;
        chart.deviceData = null;
        chart.devices = [];
        chart.clickedObjects = [];//保存可点击对象
        chart.inPortData = [];
        chart.inDevices = [];
        chart.outPortData = [];
        chart.outDevices = [];
        chart.logicConnectData.list.sort(function(a,b){
            if(a.PORT_NAME > b.PORT_NAME){
                return 0;
            }else if(a.PORT_NAME > b.PORT_NAME){
                return 1;
            }else{
                return -1;
            }
        });
        for(var k in chart.logicConnectData.list){
            var data = chart.logicConnectData.list[k];
            if(data.UNIT_ID != unit_id){
                continue;
            }
            if(chart.deviceData == null){
                chart.deviceData = data;
            }
            if(data.PORT_DIRECTION == "Rx"){
                chart.inPortData.push(data);
            }else{
                chart.outPortData.push(data);
            }
        }
        var preDevice = null;
        for(var k in chart.inPortData){
            var data = chart.inPortData[k];
            if(preDevice == null || preDevice.data.EXT_UNIT_ID != data.EXT_UNIT_ID){
                var device = {};
                device.data = data;
                chart.inDevices.push(device);
                data.device = device;
                preDevice = device;
                device.portCount=1;
                data.needDrawDevice = true;
            }else{
                data.device = preDevice;
                device.portCount+=1;
                data.needDrawDevice = false;
            }
        }
        preDevice = null;
        for(var k in chart.outPortData){
            var data = chart.outPortData[k];
            if(preDevice == null || preDevice.data.EXT_UNIT_ID != data.EXT_UNIT_ID){
                var device = {};
                device.data = data;
                device.portCount = 1;
                chart.outDevices.push(device);
                data.device = device;
                preDevice = device;
                data.needDrawDevice = true;
            }else{
                data.device = preDevice;
                device.portCount+=1;
                data.needDrawDevice = false;
            }
        }
        //增加一些方法
        chart.getTotalWidth = function(){
            var w = Chart.DeviceWidth;
            if(this.inDevices.length > 0){
                w += Chart.DeviceWidth + Chart.DeviceSpace;
            }
            if(this.outDevices.length > 0){
                w += Chart.DeviceWidth + Chart.DeviceSpace;
            }
            return w;
        };
        chart.getTotalHeight = function(){
            var lHeight = this.inPortData.length * Chart.PortHeight + this.inDevices.length * Chart.TitleHeight + (this.inDevices.length > 0 ? this.inDevices.length - 1 : 0) * Chart.DeviceVerSpace + (this.inPortData.length - this.inDevices.length) * Chart.PortVerSpace;
            var rHeight = this.outPortData.length * Chart.PortHeight + this.outDevices.length * Chart.TitleHeight + (this.outDevices.length > 0 ? this.outDevices.length - 1 : 0) * Chart.DeviceVerSpace + (this.outPortData.length - this.outDevices.length) * Chart.PortVerSpace;
            return Math.max(lHeight, rHeight);
        }
        chart.draw = function(canvas){
            Chart.draw(this, canvas);
        };
        chart.getPortByPoint = function(x ,y){
            for(var i=0;i<this.clickedObjects.length;i++){
                //1.点击的是不是此交换机接口？
                var p = this.clickedObjects[i];
                //上半部分是of_id
                if(p.x < x && p.x + p.w > x && p.y < y && p.y + p.h > y){
                    return p;
                }
            }
            return null;
        }
        return chart;
    },
    drawTitle : function(x,y,unit_name,canvas){
        //绘制标题背景
        canvas.fillStyle = Chart.TitleBg;
        canvas.fillRect(x, y, Chart.DeviceWidth, Chart.TitleHeight);
        //小图标
        var img = new Image();    
        img.src = config.icon_device_url;
        if(img.complete){
            canvas.drawImage(img,x, y+5, Chart.iconWidth , Chart.iconHeight);
        }else{
            img.onload = function(){
                canvas.drawImage(img,x, y+5, Chart.iconWidth , Chart.iconHeight);
            }
        }
        //绘制标题
        canvas.font = Chart.TitleFontSize + "px " + Device.headerFontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="left";
        canvas.fillText(unit_name,x + Chart.iconWidth, y + Chart.TitleHeight / 2 + Chart.TitleFontSize / 2 - 3, Chart.DeviceWidth - Chart.iconWidth);
    },
    drawDevicePort : function(x,y,leftPorts,rightPorts,chart){
        var r = Math.max(leftPorts.length, rightPorts.length);
        //绘制背景
        canvas.fillStyle = '#cccccc';
        canvas.fillRect(x, y, Chart.DeviceWidth, chart.getTotalHeight() - Chart.TitleHeight);
        var deviceIndex = 0;
        var preDevice = null;
        for(var i = 0; i<leftPorts.length;i++){
            if(preDevice == null){
                preDevice = leftPorts[i].device;
            }else if(preDevice.data.EXT_UNIT_ID != leftPorts[i].device.data.EXT_UNIT_ID){
                preDevice = leftPorts[i].device;
                deviceIndex++;
            }
            var cy = y + i * Chart.PortHeight + (i-deviceIndex) * Chart.PortVerSpace + deviceIndex * (Chart.DeviceVerSpace+Chart.TitleHeight);
            Chart.drawPort(x,cy,'left',leftPorts[i],canvas);
            leftPorts[i].device.data.PORT_DIRECTION = 'Tx';
            leftPorts[i].device.data.PORT_NAME = leftPorts[i].device.data.EXT_PORT_NAME;
            Chart.drawPort(x - Chart.DeviceSpace - Chart.DeviceWidth / 2, cy, 'right', leftPorts[i].device.data,canvas);
            Chart.drawLiner(
                x - Chart.DeviceSpace, 
                cy + Chart.PortHeight / 2,
                x,
                cy + Chart.PortHeight / 2,
                leftPorts[i],
                chart,
                canvas);
        }
        var deviceIndex = 0;
        var preDevice = null;
        for(var i = 0; i<rightPorts.length;i++){
            if(preDevice == null){
                preDevice = rightPorts[i].device;
            }else if(preDevice.data.EXT_UNIT_ID != rightPorts[i].device.data.EXT_UNIT_ID){
                preDevice = rightPorts[i].device;
                deviceIndex++;
            }
            var cy = y + i * Chart.PortHeight + (i-deviceIndex) * Chart.PortVerSpace + deviceIndex * (Chart.DeviceVerSpace+Chart.TitleHeight);
            rightPorts[i].x = x;
            rightPorts[i].y = y;
            Chart.drawPort(x + Chart.DeviceWidth / 2,cy,'right',rightPorts[i],canvas);
            rightPorts[i].device.data.PORT_DIRECTION = 'Rx';
            rightPorts[i].device.data.PORT_NAME = rightPorts[i].device.data.EXT_PORT_NAME;
            Chart.drawPort(x + Chart.DeviceSpace + Chart.DeviceWidth, cy, 'left', rightPorts[i].device.data,canvas);
            Chart.drawLiner(
                x + Chart.DeviceSpace + Chart.DeviceWidth, 
                cy + Chart.PortHeight / 2,
                x + Chart.DeviceWidth,
                cy + Chart.PortHeight / 2,
                rightPorts[i],
                chart,
                canvas);
        }
    },
    draw : function(chart, canvas){
        var left = chart.x;
        if(chart.inDevices.length > 0){
            left = chart.x + Chart.DeviceWidth + Chart.DeviceSpace;
        }
        chart.clickedObjects = [];
        Chart.drawTitle(left, chart.y, chart.deviceData.IED_DESC, canvas);
        var cY = chart.y;
        for(var i=0;i<chart.inPortData.length;i++){
            var data = chart.inPortData[i];
            if(data.needDrawDevice == true){
                Chart.drawTitle(left - Chart.DeviceWidth - Chart.DeviceSpace,cY, data.EXT_IED_DESC, canvas);
                var r = data.device.portCount;
                canvas.fillStyle = '#cccccc';
                canvas.fillRect(left - Chart.DeviceWidth - Chart.DeviceSpace,cY + Chart.TitleHeight, Chart.DeviceWidth, r * Chart.PortHeight + (r-1)*Chart.PortVerSpace);
                cY += r * Chart.PortHeight + (r-1)*Chart.PortVerSpace + Chart.TitleHeight + Chart.DeviceVerSpace;
            }
        }
        var cY = chart.y;
        for(var i=0;i<chart.outPortData.length;i++){
            var data = chart.outPortData[i];
            if(data.needDrawDevice == true){
                Chart.drawTitle(left + Chart.DeviceWidth + Chart.DeviceSpace,cY, data.EXT_IED_DESC, canvas);
                var r = data.device.portCount;
                canvas.fillStyle = '#cccccc';
                canvas.fillRect(left + Chart.DeviceWidth + Chart.DeviceSpace,cY + Chart.TitleHeight, Chart.DeviceWidth, r * Chart.PortHeight + (r-1)*Chart.PortVerSpace);
                cY += r * Chart.PortHeight + (r-1)*Chart.PortVerSpace + Chart.TitleHeight + Chart.DeviceVerSpace;
            }
        }
        Chart.drawDevicePort(left, chart.y + Chart.TitleHeight,chart.inPortData,chart.outPortData,chart);
    },
    drawPort:function(x,y,portDirection,data,canvas){
        var cx,cy;
        if(portDirection == "left"){
            cx = x;
            cy = y;
        }else{
            cx = x + Chart.DeviceWidth / 2 - Chart.PortHeight;
            cy = y;
        }
        var img = new Image();    
        if(portDirection == "left"){
            if(data.PORT_DIRECTION == "Rx"){
                img.src = config.icon_left_in_url;
            }else if (data.PORT_DIRECTION == "Tx"){
                img.src = config.icon_left_out_url;
            }else{
                img.src = config.icon_left_in_out_url;
            }
        }else{
            if(data.PORT_DIRECTION == "Rx"){
                img.src = config.icon_right_in_url;
            }else if (data.PORT_DIRECTION == "Tx"){
                img.src = config.icon_right_out_url;
            }else{
                img.src = config.icon_right_in_out_url;
            }
        }
        if(img.complete){
            canvas.drawImage(img,cx + 1, cy + 1, Chart.PortHeight - 3, Chart.PortHeight - 3);
        }else{
            img.onload = function(){
                canvas.drawImage(img,cx + 1, cy + 1, Chart.PortHeight - 3, Chart.PortHeight - 3);
            }
        }
        //绘制背景
        canvas.fillStyle = '#a3c4c1';
        canvas.fillRect(x, y, Chart.DeviceWidth / 2, Chart.PortHeight);
        //画线
        canvas.beginPath();
        canvas.strokeStyle = '#6699cc';
        canvas.lineWidth=1;
        if(portDirection == "left"){
            canvas.moveTo(x + Chart.PortHeight,y);
            canvas.lineTo(x + Chart.PortHeight,y + Chart.PortHeight);
        }else{
            canvas.moveTo(x,y);
            canvas.lineTo(x,y + Chart.PortHeight);
        }
        canvas.lineWidth=1;
        canvas.stroke();

        canvas.beginPath();
        canvas.strokeStyle = '#6699cc';
        canvas.lineWidth=Chart.PortVerSpace;
        canvas.moveTo(x,y);
        canvas.lineTo(x + Chart.DeviceWidth / 2 ,y);
        canvas.moveTo(x,y + Chart.PortHeight);
        canvas.lineTo(x + Chart.DeviceWidth / 2 ,y + Chart.PortHeight1);
        canvas.stroke();
        //画出名字
        canvas.font = Device.headerFontSize + "px " + Device.fontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="center";
        if(portDirection == "left"){
            canvas.fillText(data.PORT_NAME,x + Chart.PortHeight + (Chart.DeviceWidth - Chart.PortHeight * 2) / 4, y + Chart.PortHeight / 2 + Device.headerFontSize / 2, Chart.DeviceWidth - Chart.PortHeight * 2);
            chart.clickedObjects.push({
                x:x,
                y:y,
                w:Chart.DeviceWidth / 2,
                h:Chart.PortHeight,
                TYPE:'Port',
                data:data
            });
            //分割线
            canvas.beginPath();
            canvas.strokeStyle = '#6699cc';
            canvas.lineWidth=1;
            canvas.moveTo(x + Chart.DeviceWidth / 2,y);
            canvas.lineTo(x + Chart.DeviceWidth / 2,y + Chart.PortHeight);
            canvas.stroke();
        }else{
            canvas.fillText(data.PORT_NAME,x + (Chart.DeviceWidth - Chart.PortHeight * 2) / 4, y + Chart.PortHeight / 2 + Device.headerFontSize / 2, Chart.DeviceWidth - Chart.PortHeight * 2);
            chart.clickedObjects.push({
                x:x,
                y:y,
                w:Chart.DeviceWidth / 2,
                h:Chart.PortHeight,
                TYPE:'Port',
                data:data
            });
            //分割线
            canvas.beginPath();
            canvas.strokeStyle = '#6699cc';
            canvas.lineWidth=1;
            canvas.moveTo(x,y);
            canvas.lineTo(x,y + Chart.PortHeight);
            canvas.stroke();
        }
    },
    drawLiner:function(x1,y1,x2,y2,data,chart,canvas){
        var arrowColor = config.logic_fibre_color[data.TYPE];
        //画箭头
        drawArrow(canvas, x1 , y1, (x1<x2?x2 - 6:x2 + 6), y2, 20,25, 6, arrowColor);
        //画光纤编号
        var cx = (x1<x2?x1:x2) + Math.abs(x2 - x1) / 2;
        var cy = y1;
        var cw = Math.abs(x2 - x1) / 2;
        var ch = Device.headerFontSize * 2;
        cx -= cw/2;
        cy -= ch/2;
        canvas.fillStyle = Chart.TitleBg;;
        canvas.fillRect(cx, cy, cw, ch);
        canvas.fillStyle = "#35857e";
        canvas.fillRect(cx+2, cy+2, cw-4, ch-4);
        canvas.font = Device.headerFontSize + "px " + Device.fontFamily;
        canvas.fillStyle = Device.headerFontColor;
        canvas.textAlign="center";
        canvas.fillText(data.EXT_CB_NAME,cx + cw/2, cy+ch/2+Device.headerFontSize/2-4, cw);
        chart.clickedObjects.push({
            x:cx,
            y:cy,
            w:cw,
            h:ch,
            TYPE:'DEVICE-FIBRE',
            data:data
        });
    }
};